const app = getApp();
var api = require('../../api.js');

var interval = null
Page({
  data: {                                               //Object  页面的初始数据
    disabled: false,
    background: "../../images/bg.jpg",
    time: "获取验证码",
    currentTime: 60,
    phone:'',
    vcode:'',
    judge:false,
    register:false
  },
  onLoad: function(options) { 
    // app.bind();
    // app.login();                          //function 生命周期函数--监听页面加载
   
  },
  onReady: function() {                                 //function 生命周期函数--监听页面初次渲染完成
  },
  onShow: function() {                                  //function 生命周期函数--监听页面显示
  },
  onHide: function() {                                  //function 生命周期函数--监听页面隐藏
  },
  onUnload: function() {                                //function 生命周期函数--监听页面卸载
  },
  onPullDownRefresh: function() {                       //function 页面相关事件处理函数--监听用户下拉动作
  },
  onReachBottom: function() {                           //function 页面上拉触底事件的处理函数
  },
  onShareAppMessage: function () { 
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };                     //function 用户点击右上角转发
  },
  onPageScroll: function() {                            //function 页面滚动触发事件的处理函数
  },

  getCode: function (options){
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime+'秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval);
        that.setData({
          time: '重新发送',
          currentTime:60,
          disabled: false
        })
      }
    }, 1000)
  },
  
  //获取手机号码
  realnameConfirm:function(e){
    this.setData({
      phone:e.detail.value
    })
  },
  
  // 获取验证吗
  getVcode: function() {
    var that = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    console.log(that.data.phone.length)
      var access_token = wx.getStorageSync('access_token');

      if (access_token == ''){
        wx.redirectTo({
          url: '/pages/grant/grant',
        })
      }else{

        if (that.data.phone.length != 11 ) {
          wx.showToast({
            title: '请输入正确的手机号码',
            icon: 'none',
          });
          that.setData({
            judge: false
          })
        } else {
            that.setData({
              judge: true
            });
            that.setData({
              time: '60秒'
            });
            that.getCode();
            that.setData({
              disabled: true
            });
            console.log(this.data.phone);
            wx.request({
              url: api.passport.sms,
              method: "post",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                mobile: this.data.phone,
                access_token: access_token
              },
              success: function (res) {
                console.log(res)
              }
            }); 
          } 
    }

    
  },

  
  //注册
  sendValue: function(e) {
    console.log(e);
    var page = this;
    page.setData({
      phone:e.detail.value.phone,
      vcode:e.detail.value.vcode,
    });
    var access_token = wx.getStorageSync('access_token');
    if (access_token == ''){
      wx.redirectTo({
        url: '/pages/grant/grant',
      })
    }else{

      var parent = wx.getStorageSync('parent');
      var bindparent_id = wx.getStorageSync('bindparent_id');

      wx.request({
        url: api.passport.yzm,
        method: "post",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          mobile: page.data.phone,
          yzm: page.data.vcode,
          access_token: access_token,
          form_id:e.detail.formId,
          parent_id: bindparent_id == 'undefined' ? 0 :bindparent_id
        },
        success: function (res) {

          if (bindparent_id && parent == '无人介绍') {

            app.bindparent(bindparent_id);

          } else if (!bindparent_id && parent == '无人介绍') {

            app.bindparent(18);
          }


          if (res.data.code == 0) {
            page.setData({
              register: true
            });
            wx.navigateBack({
              delta: 2
            })
          } else if (res.data.code == 1){
            wx.showToast({
              title: '请输入正确验证码',
              icon: 'none',
            });
            
          }

        }
      });
    }
  },
  

  //一键获取
  getPhoneNumber: function (e) {
    console.log(e)
    var access_token = wx.getStorageSync('access_token');
    var encrypted_data = wx.getStorageSync('encryptedData');
    var iv = wx.getStorageSync('iv');
    console.log(encrypted_data);
    console.log(iv);
    if (access_token == '') {
      wx.redirectTo({
        url: '/pages/grant/grant',
      })
    } else {

      var bindparent_id = wx.getStorageSync('bindparent_id');
      var parent = wx.getStorageSync('parent');

      wx.request({
        url: api.passport.hm,
        method: "post",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          iv: e.detail.iv,
          encrypted_data: e.detail.encryptedData,
          access_token: access_token,
          parent_id: bindparent_id == 'undefined' ? 0 : bindparent_id
        },
        success: function (res) {
          console.log(res)
      
          if (bindparent_id && parent == '无人介绍') {

            app.bindparent(bindparent_id);

          } else if (!bindparent_id && parent == '无人介绍') {
            //999
            app.bindparent(18);
          }


          if (res.data.code == 0){
            wx.navigateBack({
              delta: 2
            })
          }

        }
      });
    }
  }
})